package com.lzw.service.impl;

import com.lzw.bean.RoleMenu;
import com.lzw.bean.UserRole;
import com.lzw.common.bean.Result;
import com.lzw.common.service.impl.BaseServiceImpl;
import com.lzw.mapper.RoleMenuMapper;
import com.lzw.mapper.UserRoleMapper;
import com.lzw.service.RoleMenuService;
import com.lzw.service.UserRoleService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 *
 * 角色菜单相关服务
 * Created by Administrator on 2017/6/30 0030.
 */
@Service("roleMenuService")
public class RoleMenuServiceImpl extends BaseServiceImpl<RoleMenuMapper,RoleMenu> implements RoleMenuService {


    @Override
    public Result insertAll(List<RoleMenu> roleMenus) {
        if(baseMapper.insertAll(roleMenus)>=1){
            return  this.success("插入多条数据成功");
        }else{
            return  this.error("插入多条数据失败");
        }
    }

    @Override
    public Result deleteOther(int role_id, int[] menu_ids) {
        if(baseMapper.deleteOther(role_id,menu_ids)>=1){
            return  this.success("删除其他数据成功");
        }else{
            return  this.error("删除其他数据失败");
        }
    }

    @Override
    public Result setMenu(int role_id, int[] menu_ids){
        Result result1=this.deleteOther(role_id,menu_ids);
        Result result2=null;
        if(menu_ids!=null&&menu_ids.length>0){
            List<RoleMenu> list=new ArrayList<>();
            for (int i=0;i<menu_ids.length;i++){
                if(this.findByRoleIdMenuId(role_id,menu_ids[i])==null){
                    RoleMenu roleMenu=new RoleMenu();
                    roleMenu.setRoleId(role_id);
                    roleMenu.setMenuId(menu_ids[i]);
                    list.add(roleMenu);
                }

            }
            if(list.size()>0)
             result2=this.insertAll(list);
        }
        if(result1.getCode()==0||(result2!=null&&result2.getCode()==0)){
            return  this.success("授权成功！");
        }else{
            return  this.error("授权失败！");
        }

    }

    @Override
    public UserRole findByRoleIdMenuId(int role_id, int menu_id) {
        return baseMapper.findByRoleIdMenuId(role_id,menu_id);
    }
}
